/**
 * ListPictureServiceImp.java
 *
 *
 * $LastChangedBy: allen $
 * $LastChangedDate: 2007-12-11 17:22:48 +0800 $
 * $Revision: 1440 $
 */
package com.myspace.service.picture;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;

import org.apache.commons.lang.time.DateUtils;

import com.myspace.dao.PictureCommentDao;
import com.myspace.dao.PictureDao;
import com.myspace.dao.TagDao;
import com.myspace.dao.UserDao;
import com.myspace.domain.Picture;
import com.myspace.domain.PictureComment;
import com.myspace.domain.Tag;
import com.myspace.domain.User;
import com.myspace.common.exception.SystemException;
import com.myspace.common.util.Formatter;

public class ViewPictureServiceImp implements ViewPictureService {

	private UserDao userDao;

	private PictureDao picDao;

	private TagDao tagDao;

	private PictureCommentDao picCommentDao;

	public List<Picture> getPicturesByDate(User user, Date fromDate,
			Date toDate, int pageNum, int pageSize) {
		if (user == null) {
			throw new SystemException("User not existed");
		}
		return picDao.getPictureListByDate(user, fromDate, toDate, pageNum,
				pageSize);
	}

	public List<Picture> getPicturesByTag(User user, String tag, int pageNum,
			int pageSize) {
		if (user == null) {
			throw new SystemException("User not existed");
		}
		return picDao.getPictureListByTag(user, tag, pageNum, pageSize);
	}

	public List<List<String>> getPictrueInfo(User user) {
		if (user == null) {
			throw new SystemException("User not existed");
		}
		List<Picture> list = picDao.getPictureList(user);
		List<List<String>> ret = new ArrayList<List<String>>(2);

		Set<Date> dateSet = new TreeSet<Date>();
		Set<String> tagSet = new TreeSet<String>();
		for (Picture picture : list) {
			for (Tag tag : picture.getTags()) {
				tagSet.add(tag.getName());
			}
			dateSet.add(DateUtils.truncate(picture.getCreateDate(),
					Calendar.DAY_OF_MONTH));
		}

		List<String> dateList = new ArrayList<String>(dateSet.size());
		for (Date date : dateSet) {
			dateList.add(0, Formatter.formatYYYYMMDD(date));
		}
		ret.add(dateList);

		List<String> tagList = new ArrayList<String>(tagSet.size());
		for (String tag : tagSet) {
			tagList.add(tag);
		}
		ret.add(tagList);
		return ret;
	}

	public int getTotalByDate(Long userId, Date fromDate, Date toDate) {
		return picDao.getPictureNumByDate(userId, fromDate, toDate);
	}

	public int getTotalByTag(Long userId, String tag) {
		return picDao.getPictureNumByTag(userId, tag);
	}

	public List<PictureComment> getComments(Long picId) {
		return picCommentDao.findCommentsList(picId);
	}

	public Picture getPicture(Long id) {
		return picDao.findById(id);
	}

	public UserDao getUserDao() {
		return userDao;
	}

	public void setUserDao(UserDao userDao) {
		this.userDao = userDao;
	}

	public PictureDao getPicDao() {
		return picDao;
	}

	public void setPicDao(PictureDao picDao) {
		this.picDao = picDao;
	}

	public TagDao getTagDao() {
		return tagDao;
	}

	public void setTagDao(TagDao tagDao) {
		this.tagDao = tagDao;
	}

	public PictureCommentDao getPicCommentDao() {
		return picCommentDao;
	}

	public void setPicCommentDao(PictureCommentDao picCommentDao) {
		this.picCommentDao = picCommentDao;
	}

}
